import 'package:flutter/material.dart';
import 'package:flutter_map/flutter_map.dart';
import 'package:get/get.dart';
import 'package:latlong2/latlong.dart';
import 'package:situation/app/theme/gaps.dart';
import 'package:situation/resources/assets.dart';
import 'package:situation/widgets/mk_load_image.dart';
import 'package:tdesign_flutter/tdesign_flutter.dart';

class MapPointDraw extends StatefulWidget {
  const MapPointDraw({super.key, this.onPointsChanged, this.onSave, this.onClose});

  final ValueChanged<List<LatLng>>? onPointsChanged;
  final Function(LatLng)? onSave;
  final Function()? onClose;

  static late final GlobalKey<_MapPointDrawState> stateKey =
  GlobalKey<_MapPointDrawState>();

  static void addPoint(LatLng point) {
    stateKey.currentState?._addPoint(point);
  }
  static void removeAll() {
    stateKey.currentState?.removeAll();
  }

  @override
  State<MapPointDraw> createState() => _MapPointDrawState();
}


class _MapPointDrawState extends State<MapPointDraw> {
  final List<LatLng> _points = [];

  bool _showTool = false;

  // 修改为内部公共方法
  void _addPoint(LatLng point) {

    if (!mounted) return;
    setState(() {
      _showTool=true;
      _points.add(point);
    });
    widget.onPointsChanged?.call(_points);
  }

  void removeAll() {
    setState(() {
      _points.clear();
    });
  }

  void _removePoint(int index) {
    setState(() {
      _points.removeAt(index);
    });
  }

  Widget _buildIconButton({
    required IconData icon,
    required Color color,
    required VoidCallback onPressed
  }) {
    return GestureDetector(
      onTap: onPressed,
      child: Container(
        padding: EdgeInsets.all(6),
        decoration: BoxDecoration(
          color: Colors.white,
          shape: BoxShape.circle,
        ),
        child: Icon(icon, size: 20, color: color),
      ),
    );
  }

  Widget _buildControlPanel() {
    return Positioned(
      right: 20,
      bottom: Get.height*0.4,
      child: Container(
        padding: _points.isEmpty ? const EdgeInsets.all(0) :const EdgeInsets.all(8),
        decoration: BoxDecoration(
          color: Colors.black.withAlpha(80),
          borderRadius: BorderRadius.circular(8),
        ),
        child: Column(
          children: [
            if (_showTool) ...[
              _buildIconButton(
                  icon: Icons.close,
                  color: Colors.red,
                  onPressed: () {
                    widget.onClose?.call();
                    setState(() {
                      _showTool=false;
                    });
                  }
              ),
            ],

          ],
        ),
      ),
    );
  }

  @override
  Widget build(BuildContext context) {
    return Stack(
      children: [
        MarkerLayer(
          markers: [
            for (int i = 0; i < _points.length; i++)
              Marker(
                alignment: Alignment.topCenter,
                width: 180,
                height: 109,
                point: _points[i],
                child: Column(
                  children: [
                    Container(
                      alignment: Alignment.center,
                      width: 180,
                      padding: const EdgeInsets.all(8),
                      decoration: BoxDecoration(
                        color: Colors.black87.withAlpha(120),
                        borderRadius: BorderRadius.circular(10),
                      ),
                      child: Column(
                        crossAxisAlignment: CrossAxisAlignment.start,
                        children: [
                          Row(
                            mainAxisAlignment: MainAxisAlignment.end,
                            children: [
                              GestureDetector(
                                onTap: () => {
                                  if( widget.onSave != null){
                                    widget.onSave!(_points[i]),
                                  }
                                },
                                child: Icon(Icons.save, size: 16, color: Colors
                                    .blue,),
                              ),
                              Gaps.hGap10,
                              GestureDetector(
                                onTap: () => _removePoint(i),
                                child: Icon(Icons.delete, size: 16, color: Colors
                                    .red),
                              )
                            ],
                          ),
                          Text("名称：暂未设置", style: const TextStyle(
                              color: Colors.white, fontSize: 10),),
                          Text("经度：${_points[i].longitude}",
                            style: const TextStyle(
                                color: Colors.white, fontSize: 10),),
                          Text("纬度：${_points[i].latitude}",
                            style: const TextStyle(
                                color: Colors.white, fontSize: 10),),
                        ],
                      ),
                    ),
                    Gaps.vGap8,
                    MkLoadImage(Assets.mapMapPointActive, width: 30,),
                  ],
                ),
              ),
          ],
        ),
        _buildControlPanel(),
      ],
    );
  }
}
